package com.yangfan.thread.thread_wangwenjun03;

/**
 * @description: 线程停止 -- 以中断 API 方式停止
 * @author: yangfan
 * @date: Created in 2020/1/26 0026 10:44
 * @modified By:
 */
public class ThreadStopDiscontinue {


    public static void main(String[] args) {

        ThreadTask t = new ThreadTask();
        t.start();


        /**
         * 等待300毫秒后在进行中断
         */
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.closedThread();//对线程进行中断

    }



    private static class ThreadTask extends Thread{

        int count = 0 ;

        @Override
        public void run() {



            while (true){

                if(Thread.currentThread().isInterrupted()){//如果线程被中断
                    break;//跳出循环
                }

                System.out.println("count="+ count++);

            }

        }

        /**
         * 关闭线程的方法
         */
        protected void closedThread(){
            /**
             * 如果线程没有中断则进行中断
             */
            if(!this.isInterrupted()){
                this.interrupt();
                System.out.println("对线程进行了中断");
            }

        }

    }



}
